package com.ces.knacky.controller.tools;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ces.knacky.framework.controller.BaseController;
import com.ces.knacky.framework.exception.DataAccessException;
import com.ces.knacky.framework.model.IEntity;
import com.ces.knacky.model.Auction;
import com.ces.knacky.model.AuctionItem;
import com.ces.knacky.model.AuctionStatus;
import com.ces.knacky.model.Flag;
import com.ces.knacky.model.Item;
import com.ces.knacky.model.UserTable;
import com.ces.knacky.util.ApplicationUtils;

@Controller
public class SaveAuctionController extends BaseController {

	private String description;

	private String summary;

	private String starttime;

	private String endtime;

//	private Date startdate;
//
//	private Date enddate;
	
	private String startdate;

	public String getStartdate() {
		return startdate;
	}

	@ModelAttribute("startdate")
	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public String getEnddate() {
		return enddate;
	}

	@ModelAttribute("enddate")
	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	private String enddate;
	
	private String selectedIds;

	@RequestMapping("/saveAuction.htm")
	public String execute() {
		System.out.println("Inside Save Auction");
		System.out.println("getDescription : "+getDescription());
		System.out.println("getSummary() : "+getSummary());
		System.out.println("getStartTime() : "+getStartTime());
		System.out.println("getEndTime() : "+getEndTime());
		System.out.println("getStartDate() : "+getStartdate());
		System.out.println("getEndDate() : "+getEnddate());		
		System.out.println("getSelectedIds() : "+getSelectedIds());
		
		Auction auction = new Auction();
		auction.setDescription(description);
		auction.setSummary(summary);
		auction.setStartTime(starttime);
		auction.setEndTime(endtime);
		try {
			auction.setStartDate(ApplicationUtils.convertStringToDate(getStartdate()));
			auction.setEndDate(ApplicationUtils.convertStringToDate(getEnddate()));
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		auction.setCreateTime(new Date());
		auction.setUpdateTime(new Date());
		/*TODO - use logged-in user here */
		IEntity entity = commonService.findFirstByDescription(UserTable.class,
				"admin");
		AuctionStatus auctionStatus = (AuctionStatus) commonService.findFirstByDescription
		(AuctionStatus.class, "OPENED");
		auction.setAuctionStatus(auctionStatus);
		auction.setUser((UserTable) entity);
		entity = commonService.findFirstByDescription(Flag.class, "ACTIVE");
		auction.setFlag((Flag) entity);		
		List <Item> items = (List<Item>) commonService.list("SELECT item FROM Item item where " +
		"item.id IN ("+getSelectedIds() + " ) ");
		
		AuctionItem auctionItem = null;
		/* Create set of auction-items*/
		Set <AuctionItem> auctionItemSet = new HashSet<AuctionItem>();
		for(Item item : items){
			auctionItem = new AuctionItem();
			auctionItem.setItem(item);
			auctionItem.setFlag((Flag) entity);
			auctionItem.setAuction(auction);	
			System.out.println("adding item : "+item.getId());
			System.out.println("hashCode:"+auctionItem.hashCode());
			System.out.println("auctionItem.getId() : "+auctionItem.getId());
			System.out.println("auctionItem.toString():"+auctionItem.toString());
			boolean ret = auctionItemSet.add(auctionItem);		
			System.out.println("ret = "+ret);
		}
		System.out.println("size of auctionItemSet : "+auctionItemSet.size());
		auction.setAuctionItems(auctionItemSet);
		commonService.save(auction);
		return "saveAuction";

	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	@ModelAttribute("description")
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the summary
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * @param summary
	 *            the summary to set
	 */
	@ModelAttribute("summary")
	public void setSummary(String summary) {
		this.summary = summary;
	}

	/**
	 * @return the starttime
	 */
	public String getStartTime() {
		return starttime;
	}

	/**
	 * @param startime
	 *            the startime to set
	 */
	@ModelAttribute("startime")
	public void setStartTime(String starttime) {
		this.starttime = starttime;
	}

	/**
	 * @return the endtime
	 */
	public String getEndTime() {
		return endtime;
	}

	/**
	 * @param endtime
	 *            the endtime to set
	 */
	@ModelAttribute("endtime")
	public void setEndTime(String endtime) {
		this.endtime = endtime;
	}

//	/**
//	 * @return the startdate
//	 */
//	public Date getStartDate() {
//		return startdate;
//	}
//
//	/**
//	 * @param startdate the startdate to set
//	 * @throws DataAccessException 
//	 */
//	@ModelAttribute("startdate")
//	public void setStartDate(String startdate) throws DataAccessException {
//		System.out.println("startdate = "+startdate);
//		this.startdate = ApplicationUtils.convertStringToDate(startdate);
//		
//	}
//	/**
//	 * 
//	 * @return the enddate
//	 */
//	public Date getEndDate() {
//		return enddate;
//	}
//
//	/**
//	 * @param enddate the enddate to set
//	 * @throws DataAccessException 
//	 */
//	@ModelAttribute("enddate")
//	public void setEndDate(String enddate) throws DataAccessException {
//		System.out.println("enddate = "+enddate);
//		this.enddate = ApplicationUtils.convertStringToDate(enddate);		
//	}

	/**
	 * 
	 * @return selectedIds 
	 * 		  persistent Ids of the items selected for auction
	 */
	public String getSelectedIds() {
		return selectedIds;
	}

	/**
	 * @param selectedIds
	 	  		persistent Ids of the items selected for auction
	*/
	@ModelAttribute("selectedIds")
	public void setSelectedIds(String selectedIds) {
		this.selectedIds = selectedIds;
	}


}
